# -*- coding:utf-8 -*-

import os
import sys
import os.path

COMMIT_SRC = "//hack code by zsl\n"

FLAG_SRC_1 = "global::System.Convert.FromBase64String"
FLAG_DST_1 = COMMIT_SRC + "return;\n"

FLAG_SRC_2 = "return MergeFrom(input, pb::ExtensionRegistry.Empty);"
FLAG_DST_2 = COMMIT_SRC + "return MergeFrom(input, null);\n//"

g_dic = {FLAG_SRC_1 : FLAG_DST_1, FLAG_SRC_2 : FLAG_DST_2}

def quit(msg):
    print msg
    raw_input("press Enter to Exist...")
    exit()

def parse(in_file, out_file):
    in_stream = open(os.path.abspath(os.path.normpath(in_file)))
    if not in_stream:
        quit("can not open int file: " + in_file)

    lines = in_stream.readlines()
    lines_out = []
    lines_out.append("#pragma warning disable 0219,0162\n")
    for line in lines:
        line_out = line
        for key in g_dic.keys():
            if key in line:
                line_out = g_dic[key] + line
                break
        lines_out.append(line_out)

    out_stream = open(os.path.abspath(os.path.normpath(out_file)), "w")
    if not out_stream:
        quit("can not open out file: " + in_file)

    out_stream.writelines(lines_out)
    out_stream.flush()
    out_stream.close()

if __name__ == '__main__':
    if len(sys.argv) != 3:
        print "arg count is wrong, we need in_file and out_file"
    parse(sys.argv[1], sys.argv[2])

